<?php 
namespace Admin\Controller;
use Think\Controller;
class MasterController extends BaseController {

	/*public function sportlist(){
		$sportModel=M('sport');//表明,conf中定义前缀
		$sport=$sportModel->order('sportid asc')->select(); //desc降序 asc升序

		//dump($sport);
		$this->sport=$sport;//name属性对应的值 $this->对应值=$sport;
			
		$this->display();
	}
*/

public function masterlist(){
		$masterModel=M('master');//表明,conf中定义前缀
		//$master=$masterModel->order('master_id asc')->select(); //desc降序 asc升序
    $master=$masterModel->table('master m,master_type mt')->where('m.master_type_id=mt.master_type_id')->field('m.*,mt.master_type_name')->order('master_id asc')->select();
		//dump($sport);
		$this->master=$master;//name属性对应的值 $this->对应值=$sport;
			
		$this->display();
	}

  public function consolelist(){
    $consoleModel=M('console');//表明,conf中定义前缀
  // $console=$consoleModel->order('console_id asc')->select(); //desc降序 asc升序
  $console=$consoleModel->table('users u,master m,console c')->where('c.user_id=u.user_id and c.master_id=m.master_id')->order('console_id asc')->field('c.*,u.username,m.master_name')->select();
    //dump($sport);
    $this->console=$console;//name属性对应的值 $this->对应值=$sport;
   // var_dump($console);
      
   $this->display();
  }

public function delconsole(){
  $console_id=$_GET['console_id'];
  $consoleModel = M("console"); // 实例化sport对象
  $console=$consoleModel->where("console_id=$console_id")->select();
  $result=$consoleModel->where("console_id=$console_id")->delete(); // 删除sportid为5的用户数据
  //dump($sport);
    if ($console[0]['picture']!='nopic.jpg') {
              $picturePath='Public/Uploads/consolephoto/'.$train[0]['picture'];
              unlink($picturePath);
            }
    if($result){          
        $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
      } else {
         $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
      }



    }

public function delmaster(){
	$master_id=$_GET['master_id'];
	$masterModel = M("master"); // 实例化sport对象
	$master=$masterModel->where("master_id=$master_id")->select();
	$result=$masterModel->where("master_id=$master_id")->delete(); // 删除sportid为5的用户数据
	//dump($sport);
		if ($master[0]['picture']!='nopic.jpg') {
              $picturePath='Public/Uploads/masterphoto/'.$train[0]['picture'];
              unlink($picturePath);
            }
		if($result){					
    		$this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
			} else {
   			 $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
			}



		}
	public function like(){
		$master_id=$_GET['master_id'];
		$like=$_GET['like'];
		//dump ($isfinish);
 
		$masterModel = M("master"); // 实例化sport对象
		// 要修改的数据对象属性赋值
		switch ($like) {
			case '1':
				$data['like'] = '0';
				break;
			default:
				$data['like'] = '1';
				break;
		}
		$result=$masterModel->where("master_id=$master_id")->save($data); // 根据条件保存修改的数据

		if($result){					
    		$this->success('更改状态成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
			} else {
   			 $this->error('更改状态失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
			}

		
	}
 

  public function isrequire(){
    $console_id=$_GET['console_id'];
    $isrequire=$_GET['isrequire'];
    //dump ($isfinish);
 
    $consoleModel = M("console"); // 实例化sport对象
    // 要修改的数据对象属性赋值
    switch ($isrequire) {
      case '1':
        $data['isrequire'] = '0';
        break;
      default:
        $data['isrequire'] = '1';
        break;
    }
    $result=$consoleModel->where("console_id=$console_id")->save($data); // 根据条件保存修改的数据
//dump($data);
    if($result){          
        $this->success('更改状态成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
      } else {
         $this->error('更改状态失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
      }

    
  }
 
	
public function editmaster(){
		$master_id=$_GET['master_id'];
		$masterModel=M('master');//表明,conf中定义前缀
		$master= $masterModel->where("master_id=$master_id")->select();

		//dump($user);
		$this->master=$master;//name属性对应的值 $this->对应值=$user;
		$this->display();
	}
	 
	 

public function updatemaster(){

			$data=$_POST;
			$file=$_FILES;
			
			$master_id=$data['master_id'];

			$masterModel = M('master'); // 实例化sport模型
		

		if ($file['master_picture']['size']==0) {
            //没有修改头像
            $result=$masterModel->where("master_id=$master_id")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/masterphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
				 //保存表单数据 包括附件数据
               	$data['master_picture']= $info['master_picture']['savename']; 
                $result=$masterModel->where("master_id=$master_id")->save($data);
                	if ($data['oldpic']!='nopic.jpg') {
                	$oldpicPath='Public/Uploads/masterphoto/'.$data['oldpic'];
                	unlink($oldpicPath);
               		 	}
            		}
        	}
		if($result){
       		 $this->success('修改成功！','../master/masterlist');
     		 }
      		else{
        		$this->error('修改失败！');
      			}
	
	}



 Public function adddmaster(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['picture']['size']);
        
        //没有头像
        $masterModel = M('master'); // 实例化User模型
        $createresult=$masterModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
                 $masterModel->master_name = trim(I('post.master_name'));
                $masterModel->master_introduce = trim(I('post.master_introduce'));
                $masterModel->master_number = trim(I('post.master_number'));  
                $masterModel->master_course_id = trim(I('post.master_course_id'));
                $masterModel->master_type_id=trim(I('post.master_type_id'));
                $masterModel->like = 0; // 设置默认的用户状态 0为正常状态
                 //$masterModel->master_classfly== trim(I('post.master_classfly'));

                // $masterModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
       
             if ($file['master_picture']['size']==0) {//没有头像
                        $masterModel->master_picture = "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有头像
                            $info=$this->MyUpload();
                            $masterModel->master_picture = $info['master_picture']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$masterModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('发布培训成功！', '../master/masterlist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($masterModel->getError());  
              }

    
    }



   
  public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/masterphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                              // dump($info);    
                             //dump($info['picture']['savename']);
                             // 保存表单数据 包括附件数据
                         //处理头像图片

                $picinfo="Public/Uploads/masterphoto/".$info['master_picture']['savename'];
                $image = new \Think\Image(); 
                $image->open($picinfo);
                // 生成一个居中裁剪为150*150的缩略图并保存
                 $image->save($picinfo);
                $image->open($picinfo)->text('AppStore','Public/assets_a/font/jinglei.ttf',20,'#000000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($picinfo); 
                return $info;
                         }
    }      


}
 ?>